        
using System;
using Dapper;
using System.Linq;
using Project.IData;
using Project.Model;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace Project.Data 
{
    public partial class EmployeeData : BaseData<Employee>,IEmployeeData
    {
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity">插入数据对象</param>
        /// <returns>数据库插入条数</returns>
        public int Create(Employee entity)
        {
			string sql=@"insert into Employee(LoginName, LoginPwd) values(:LoginName, :LoginPwd)";
            using (var connection = Database.GetConnection())
            {
                return connection.Query<int>(sql, entity).FirstOrDefault();
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">修改数据对象</param>
        /// <returns>修改成功条数</returns>
        public Employee Edit(Employee entity)
        {
            string sql = @"update Employee set LoginName = :LoginName, LoginPwd = :LoginPwd where EmployeeId = :EmployeeId";
            using (var connection = Database.GetConnection())
            {
                connection.Execute(sql, entity);
            }
            return entity;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">用户对象，只传Id即可</param>
        /// <returns>删除的行数</returns>
        public int Delete(Employee entity)
        {
            string sql = "delete Employee where EmployeeId = :EmployeeId ";
            using (var connection = Database.GetConnection())
            {
                return connection.Execute(sql, entity);
            }
        }
        /// <summary>
        /// 获取所有的数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Employee> LoadModel(Expression<Func<Employee,bool>> lambdaWhere)
        {
            string sql = "select e.EmployeeId,e.LoginName,e.LoginPwd  from Employee e"+ " where " + lambdaWhere.Body.ToString().Substring(1,lambdaWhere.Body.ToString().Length-2);

            using (var connection =Database.GetConnection())
            {
				 return  connection.Query<Employee>(sql);
            }
        }
        /// <summary>
        /// 获取相应的分页数据
        /// </summary>
        /// <param name="pageIndex">第几页</param>
        /// <param name="pageCount">每页条数</param>
        /// <returns></returns>
        public IEnumerable<Employee> LoadModelByPage(int pageIndex, int pageCount,out int total, Expression<Func<Employee,bool>> lambdaWhere)
        {
var start = pageIndex * pageCount - pageCount;
            var end = pageIndex * pageCount;

            string sql = string.Format("select EmployeeId, LoginName, LoginPwd from (select row_number() over(order by u.UserInfoId desc) as rk, e.EmployeeId,e.LoginName,e.LoginPwd  from Employee e) e where rk between {0} and {1}   and {2}",start,end, lambdaWhere.Body.ToString().Substring(1,lambdaWhere.Body.ToString().Length-2));

            using (var connection = Database.GetConnection())
            {
				var userList = connection.Query<Employee>(sql);				
                
				total = userList.Count();
                return userList;
            }
        }

    }
}
